﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;
using System.Dynamic;
using FluorineFx.Json;

namespace lxwmvc
{
    /// <summary>
    /// 绑卡操作
    /// </summary>
    public static partial class huifucontroller
    {
        /// <summary>
        /// 删除绑卡信息，同步方法
        /// </summary>
        /// <param name="pb"></param>
        [json]
        public static void huifu_bangka_shanchu(pagebase pb)
        {
            var Bankid = pb.v_i("id");
            var BankCardNum = pb.v_ss("BankCardNum");
            pb.ifcheck(Bankid <= 0 || BankCardNum == "", "非法传递参数!");

            DelCard sendobj = new DelCard();
            sendobj.UsrCustId = core_user.getUsrCustId();
            sendobj.CardId = BankCardNum;

            var tobjrev = core_huifu.Send<RetDelCard>(sendobj);

            pb.ifcheck(tobjrev.RespCode == "542", "删除失败!默认银行卡不能删除!");
            {
                if (tobjrev.RespCode == core_huifu.HUIFU_SUCCESS)
                {                    
                    var ret = supermodel.create("SysUser_Bank")
                        .where("user_id='" + core_user.userID + "' and SysUser_Bankid=" + Bankid)
                        .delete();
                    if (ret > 0)
                    {
                        //删除成功
                        BaseJson json = pb.JSON<BaseJson>();
                        json.success = true;
                        json.msg = "OK";
                        return;
                    }                   
                }
            }
            
            pb.ifcheck(true, "删除失败!请重试!");
        }


        /// <summary>
        /// 汇付新增绑卡，跳到汇付的页面,target=_blank
        /// </summary>
        /// <param name="pb"></param>
        public static void huifu_bangka_add(pagebase pb)
        {
            supermodel yonghu = core_user.getUserInfo();
            dynamic sendobj = new UserBindCard();
            sendobj.Version = "10";
            sendobj.CmdId = "UserBindCard";
            sendobj.MerCustId = HuiFuArg.MerCustId;
            sendobj.UsrCustId = yonghu["UsrCustId"] + "";
            sendobj.BgRetUrl = HuiFuArg.LocalUrl + "huifu/huifu_bangka_add_return";


            supermodel SysUser_Bank = supermodel.create("SysUser_Bank");
            SysUser_Bank["truename"] = yonghu["truename"] + "";
            SysUser_Bank["cardtype"] = "身份证";
            SysUser_Bank["cardnum"] = yonghu["cardnum"] + "";
            SysUser_Bank["bankprovince"] = "";
            SysUser_Bank["bankcity"] = "";
            SysUser_Bank["user_id"] = yonghu["user_id"] + "";
            SysUser_Bank["Enable"] = "0";
            var ret = SysUser_Bank.saveorupdate();

            //插入成功后，把流水的ID传递到实体中
            sendobj.MerPriv = ret.identity + "";
            core_huifu.Submit(sendobj);
        }

        /// <summary>
        /// 绑卡后返回信息
        /// </summary>
        /// <param name="pb"></param>
        public static void huifu_bangka_add_return(pagebase pb)
        {
            RetUserBindCard objrev = new RetUserBindCard();
            //后台调用，最终要打印出来参数
            core_huifu.ReturnUrl(HttpContext.Current, "TRXID", objrev, true);

            //代表成功！
            if (objrev.RespCode == core_huifu.HUIFU_SUCCESS)
            {               
                supermodel bank = supermodel
                    .create("SysUser_Bank")
                    .select("count(1)")
                    .where("bankcardnum='" + objrev.OpenAcctId.tosafe()+"'");

                if (bank.one<int>() == 0)
                {
                    bank["banktype"] = objrev.OpenBankId + "";
                    bank["bankname"] = (objrev.OpenBankId) + "";//这个需要通过编码转换成银行的中文名称
                    bank["user_id"] = objrev.MerPriv;
                    bank["bankcardnum"] = objrev.OpenAcctId;

                    //保存银行信息
                    bank.saveorupdate();
                }


                //JSHelper.Redirect("/PUser/message.html?status=1&msg=UserBindCard");
            }
            else
            {
                //num = 0;
                //JSHelper.Redirect("/PUser/message.html?status=0&msg=UserBindCard");
            }
        }

        /// <summary>
        /// 绑卡后返回信息
        /// </summary>
        /// <param name="pb"></param>
        //public static void huifu_bangka_add_return(pagebase pb)
        //{
        //    RetUserBindCard objrev = new RetUserBindCard();
        //    //后台调用，最终要打印出来参数
        //    core_huifu.ReturnUrl(HttpContext.Current, "RECV_ORD_ID", objrev, false);
           
        //    //代表成功！
        //    if (objrev.RespCode == core_huifu.HUIFU_SUCCESS)
        //    {
        //        supermodel userinfo = core_user.getUserInfoByUsrCustId(objrev.UsrCustId);

        //        supermodel bank = supermodel
        //            .create("SysUser_Bank")
        //            .select("Enable")
        //            .where("SysUser_Bankid="+objrev.MerPriv.toint32());

        //        if(bank.one<string>() == "0")
        //        {

        //            var banktypeid = bank
        //                .select("SysUser_BankTypeid")
        //                .where("BankTypeLogo like '" + objrev.OpenBankId + "%'")
        //                .one<string>();


        //            bank
        //                .set("Enable", "1")
        //                .set("banktypeid", banktypeid)
        //                .set("bankcardnum", objrev.OpenAcctId)
        //                .where("SysUser_Bankid=" + objrev.MerPriv.toint32())
        //                .update();

        //            //发送短信
        //            core_sms.SendMsg(userinfo["MOBIL_NO"].tostr(), "亲爱的嘉金投资用户,您于" + DateTime.Now.ToString("F") + "成功绑定尾号为" + objrev.OpenAcctId.Replace(objrev.OpenAcctId.Substring(0, objrev.OpenAcctId.Length - 4), "") + "的提现银行卡。若非本人操作，请您联系致电客服。400-100-0616");
        //            //发送站内信
        //            core_user.sendMessage(userinfo["user_id"] + "", "亲爱的嘉金投资用户,您于" + DateTime.Now.ToString("F") + "成功绑定尾号为" + objrev.OpenAcctId.Replace(objrev.OpenAcctId.Substring(0, objrev.OpenAcctId.Length - 4), "") + "的提现银行卡。若非本人操作，请您联系致电客服。400-100-0616");
        //        }


        //        //JSHelper.Redirect("/PUser/message.html?status=1&msg=UserBindCard");
        //    }
        //    else
        //    {
        //        //num = 0;
        //        //JSHelper.Redirect("/PUser/message.html?status=0&msg=UserBindCard");
        //    }
        //}

        ///// <summary>
        ///// 获取省份
        ///// </summary>
        ///// <param name="pb"></param>
        //[json]
        //public static void BankProvince(pagebase pb)
        //{
        //    int id = ParamsValue("id").toint32();
        //    string text = ParamsValue("text");
        //    DataRowCollection BankProvinces = jiandansql.getrows("select BankProvince from SysUser_BankName where  [Enable]=1 and  BankTypeid=" + id + " group by BankProvince");
        //    List<Option> Options = new List<Option>();
        //    if (BankProvinces != null)
        //    {
        //        foreach (DataRow item in BankProvinces)
        //        {
        //            Options.Add(new Option() { id = id.tostr(), text = item[0].tostr() });
        //        }
        //    }
        //    string json = JsonHelper.JsonSerializer(Options);
        //    RenderText(json);
        //}
        ///// <summary>
        ///// 获取城市
        ///// </summary>
        //[SkipFilter()]
        //public void BankCity()
        //{
        //    this.CancelView();
        //    int id = ParamsValue("id").toint32();
        //    string text = ParamsValue("text");
        //    jiandansql.checkstring(text);
        //    DataRowCollection DataRows = jiandansql.getrows("select BankCity from SysUser_BankName where  [Enable]=1 and  BankTypeid=" + id + " and BankProvince='" + text + "' group by BankCity");
        //    List<Option> Options = new List<Option>();
        //    if (DataRows != null)
        //    {
        //        foreach (DataRow item in DataRows)
        //        {
        //            Options.Add(new Option() { id = id.tostr(), text = item[0].tostr() });
        //        }
        //    }
        //    string json = JsonHelper.JsonSerializer(Options);
        //    RenderText(json);
        //}
        ///// <summary>
        ///// 获取开户行 支行
        ///// </summary>
        //[SkipFilter()]
        //public void BankName()
        //{
        //    this.CancelView();
        //    int id = ParamsValue("id").toint32();
        //    string text = ParamsValue("text");
        //    jiandansql.checkstring(text);
        //    DataRowCollection DataRows = jiandansql.getrows("select SysUser_BankNameid,Bankname from SysUser_BankName where [Enable]=1 and BankTypeid=" + id + " and BankCity='" + text + "' group by SysUser_BankNameid,Bankname");
        //    List<Option> Options = new List<Option>();
        //    if (DataRows != null)
        //    {
        //        foreach (DataRow item in DataRows)
        //        {
        //            Options.Add(new Option() { id = item[0].tostr(), text = item[1].tostr() });
        //        }
        //    }
        //    string json = JsonHelper.JsonSerializer(Options);
        //    RenderText(json);
        //}



    }
}